package com.zcg.gasmanager.dao.impl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.zcg.gasmanager.dao.UserDao;
import com.zcg.gasmanager.model.Gas;
import com.zcg.gasmanager.model.Repair;
import com.zcg.gasmanager.model.TYPE;
import com.zcg.gasmanager.model.User;
import com.zcg.gasmanager.utils.HibernateUtil;

@Component("userDao")
public class UserDaoImpl implements UserDao {

	private HibernateTemplate hibernateTemplate;

	@Override
	public void save(User u) throws RuntimeException {
		hibernateTemplate.save(u.getGas());
		hibernateTemplate.save(u);
	}

	@Override
	public void delete(User u) throws RuntimeException {
		User user = (User) hibernateTemplate.get(User.class, u.getId());
		if (user != null) {
			hibernateTemplate.delete(user);
		}
	}

	@Override
	public List<User> queryList() throws RuntimeException {
		List<User> users = hibernateTemplate.find("from User");
		return users;
	}

	@Override
	public User queryById(User u) throws RuntimeException {
		u = (User) hibernateTemplate.load(User.class, u.getId());
		return u;
	}

	@Override
	public void update(User u) throws RuntimeException {
		User user = (User) hibernateTemplate.get(User.class, u.getId());
		user.setAddress(u.getAddress());
		user.setPhone(u.getPhone());
		user.setName(u.getName());
		user.setNotes(u.getNotes());
		user.setUserType(u.getUserType());
		hibernateTemplate.update(user);
	}

	@Override
	public boolean load(User u) throws RuntimeException {
		List<User> users = hibernateTemplate.find(
				"from User u where u.name = ? and u.phone = ?", new Object[] {
						u.getName(), u.getPhone() });
		boolean isUser = users.size() >= 1;
		return isUser;
	}

	@Override
	public User loadById(int id) throws RuntimeException {
		List<User> users = hibernateTemplate.find("from User u where u.id = ?",
				id);
		return users.get(0);
	}

	@Override
	public boolean loadName(User u) throws RuntimeException {
		List<User> users = hibernateTemplate.find(
				"from User u where u.name = ?", u.getName());
		;
		boolean isUser = users.size() >= 1;
		return isUser;
	}

	@Override
	public void update(TYPE userType) throws RuntimeException {
		Session session = HibernateUtil.getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("update User u set u.userType = ? ")
				.setParameter(0, userType);
		q.executeUpdate();
		session.getTransaction().commit();
	}

	@Override
	public List<User> queryLimit(final int start, final int limit) {
		List<User> users = hibernateTemplate
				.executeFind(new HibernateCallback() {

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query q = session.createQuery("from User");
						q.setFirstResult((start - 1) * 10);
						q.setMaxResults(limit);
						return q.list();
					}
				});
		return users;
	}

	@Override
	public List<User> queryLimitByName(final int start, final int limit,
			final String name) {
		List<User> users = hibernateTemplate
				.executeFind(new HibernateCallback() {

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query q = session
								.createQuery("from User where name like ?");
						q.setString(0, "%" + name + "%");
						q.setFirstResult((start - 1) * 10);
						q.setMaxResults(limit);
						return q.list();
					}
				});
		return users;
	}

	@Override
	public int getCount() {
		return (int) (long) hibernateTemplate.find("select Count(*) from User")
				.get(0);
	}

	@Override
	public List<Gas> queryGasAll() throws RuntimeException {
		List<Gas> gass = hibernateTemplate.find("from Gas");
		return gass;
	}

	@Override
	public List<User> queryLimitOrder(final int page, final int rows,
			final String sort, final String order) {
		List<User> users = hibernateTemplate
				.executeFind(new HibernateCallback() {

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query q = session.createQuery("from User order by "+sort+" "+order);
						q.setFirstResult((page - 1) * 10);
						q.setMaxResults(rows);
						return q.list();
					}
				});
		return users;
	}

	@Override
	public List<User> queryLimitOrderByName(final int page, final int rows,
			final String sort, final String order, final String name) {
		List<User> users = hibernateTemplate
				.executeFind(new HibernateCallback() {

					@Override
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query q = session
								.createQuery("from User u where u.name like :name order by "+sort+" "+order);
						q.setParameter("name", "%" + name + "%");
						q.setFirstResult((page - 1) * 10);
						q.setMaxResults(rows);
						return q.list();
					}
				});
		return users;
	}

	@Override
	public void deleteIds(String ids) {
		List list = hibernateTemplate.find("from User where id In("+ids+")");
		if(list.size()>0){
			hibernateTemplate.deleteAll(list);
		}
	}
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

}
